<template>
  <div class="channel-editer">
    <van-cell
      class="textTitle"
      slot="title"
      title="我的频道"
      :border="false"
    >
      <van-button
        class="editerBtn"
        plain
        round
        size="mini"
        type="danger"
        @click="isEdit = !isEdit"
      >{{isEdit ? '完成' : '编辑'}}</van-button>
    </van-cell>
    <van-grid
      class="myGrid"
      :gutter="10"
    >
      <van-grid-item
        class="gridItem"
        v-for="(channel, index) in mychannels"
        :key="index"
        @click="onMyChannelClick(channel, index)"
      >
        <van-icon
          v-show="isEdit && !fiexChannels.includes(channel.id)"
          slot="icon"
          name="clear"
        ></van-icon>
        <span
          class="text"
          slot="text"
          :class="{active: index === active}"
        >{{channel.name}}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell
      class="textTitle"
      slot="title"
      title="频道推荐"
      :border="false"
    />
    <van-grid
      class="reGrid"
      :gutter="10"
    >
      <van-grid-item
        class="gridItem"
        icon="plus"
        v-for="(channel,index) in recommendChannels"
        :key="index"
        :text="channel.name"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>
<!--/ 频道推荐 -->
<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'

export default {
  name: 'ChannelAditer',
  props: {
    mychannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false, // 控制编辑状态的显示
      fiexChannels: [0] // 固定频道 不允许删除
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      return this.allChannels.filter(channel => {
        return !this.mychannels.find(myChannel => {
          return myChannel.id === channel.id
        })
      })
      //   const channels = []
      //   this.allChannels.forEach(channel => {
      //     const ret = this.mychannels.find(mychannel => {
      //       return mychannel.id === channel.id
      //     })
      //     if (!ret) {
      //       channels.push(channel)
      //     }
      //   })
      //   return channels
      // }
    }
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        console.log(data)
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('获取失败')
      }
    },
    async onAddChannel (channel) {
      this.mychannels.push(channel)

      // 数据持久化处理
      if (this.user) {
        try {
          // 已登录 把数据请求接口放到线上
          await addUserChannel({
            id: channel.id,
            seq: this.mychannels.length
          })
        } catch (err) {
          this.$toast('获取失败稍后重试')
        }
      } else {
        // 未登录 把数据存到本地
        setItem('TOUTIAO_CHANNELS', this.mychannels)
      }
    },
    onMyChannelClick (channel, index) {
      // 编辑删除 非编辑切换
      if (this.isEdit) {
        if (this.fiexChannels.includes(channel.id)) {
          return
        }
        if (index <= this.active) {
          //  让激活频道的索引-1
          this.$emit('updateActive', this.active - 1, true)
        }
        this.mychannels.splice(index, 1)

        // 处理持久化
        this.deleteChannel(channel)
      } else {
        this.$emit('updateActive', index, false)
      }
    },

    // 处理持久化
    async deleteChannel (channel) {
      try {
        if (this.user) {
          // 已登陆
          await deleteUserChannel(channel.id)
        } else {
          // 未登录 把数据存到本地
          setItem('TOUTIAO_CHANNELS', this.userChannels)
        }
      } catch (err) {
        this.$toast('操作失败，请稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-editer {
  padding: 83px 0;
  .textTitle {
    font-size: 32px;
    color: #333;
  }
  .editerBtn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .gridItem {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        color: #222;
        font-size: 28px;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .myGrid {
    .gridItem {
      .van-grid-item__content {
        white-space: nowrap;
        .van-icon-clear {
          position: absolute;
          right: -10px;
          top: -10px;
          font-size: 36px;
          color: #ccc;
        }
      }
    }
  }

  /deep/.reGrid {
    .gridItem {
      .van-grid-item__content {
        white-space: nowrap;
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
